<?php
/*
  This page has a listing or potential trade properties.  But it doesn't really do more than simply list them.
  Like the other pages, if a user is registered, they will see more details.   Otherwise, not.
  It does double duty for both intro & pro.
*/
?>

<?php
  $thumbnail = 'img/blank.png';
  if ($_REQUEST['page'] == 'introTrading') {
    if (!isset($_COOKIE['ys-parcelNum'])) {
      setcookie('noParcelNum',true);
      $u = explode('/',$_SERVER['REQUEST_URI']);
      array_pop($u);
      $u = 'http://'.$_SERVER['SERVER_NAME'].implode('/',$u).'/?page=introLocation';
      header('Location: '.$u);
    }
    else if (!isset($_COOKIE['ys-pollution'])) {
      setcookie('noPollution',true);
      $u = explode('/',$_SERVER['REQUEST_URI']);
      array_pop($u);
      $u = 'http://'.$_SERVER['SERVER_NAME'].implode('/',$u).'/?page=introPollution';
      header('Location: '.$u);
    }
    if (isset($_COOKIE['ys-parcelThumbnail'])) {
      $thumbnail = substr($_COOKIE['ys-parcelThumbnail'],2);
    }
  }
  $isRegistered = isset($_SESSION['gdusername']) AND isset($_SESSION['gdpassword']);
?>

  <body onload="Ext.onReady(function(){init()})">
    <script>
      var rg;
      var gp;
      var isRegistered = <?php echo json_encode($isRegistered)?>;
      function init() {
        document.getElementById('prevStep').href = '<?php echo $_REQUEST['page'] == 'introTrading' ? '?page=introResults' : '?page=proSolution'?>';

        Ext.get('loading').fadeOut({duration : 0.2,remove : true});
        Ext.QuickTips.init();

        rg = new Ext.form.RadioGroup({
           fieldLabel : 'Show Owners with'
          ,id         : 'creditsDeficitsRadioGruop'
          ,columns    : [60,60]
          ,value      : '>'
          ,items      : [
             {boxLabel : 'Credit' ,name : 'creditDeficit',inputValue : '>'}
            ,{boxLabel : 'Deficit',name : 'creditDeficit',inputValue : '<'}
          ]
          ,listeners  : {
            afterrender : function(rg) {
              rg.addListener('change',function() {
                gp.getStore().load();
              });
            }
          }
        });

        new Ext.form.FormPanel({
           renderTo   : 'creditDeficit'
          ,border     : false
          ,labelWidth : 125
          ,fieldWidth : 50
          ,items      : rg
        });

        gp = new Ext.grid.GridPanel({
           store      : new Ext.data.JsonStore({
             url           : 'svc/getParcelsCredit.php'
            ,root          : 'data'
            ,totalProperty : 'count'
            ,idProperty    : 'ParcelNum'
            ,remoteSort    : true
            ,autoLoad      : true
            ,baseParams    : {operator : '>'}
            ,fields        : [
<?php
  if ($isRegistered) {
?>
               'Parcel Num'
              ,'Owner Name'
              ,
<?php
  }
?>
               'Credit Removal (lb/yr)'
              ,'Town'
              ,'Load Credit/Deficit (lb/yr)'
              ,'Price ($)'
              ,'Price ($/lb/yr)'
              ,'Phys Addr1'
              ,'Phys City'
              ,'Phys State'
              ,'Phys Zip'
            ]
            ,listeners  : {
              beforeload : function(sto) {
                if (rg.rendered) {
                  sto.setBaseParam('operator',rg.items.get(0).getGroupValue());
                }
              }
            }
          })
          ,columns    : [
<?php
  if ($isRegistered) {
?>
             {dataIndex : 'Parcel Num'                 ,header : 'Parcel Num'      ,width : 100                 ,sortable : true}
            ,{dataIndex : 'Owner Name'                 ,header : 'Owner Name'                  ,id : 'ownerName',sortable : true}
<?php
  }
  else {
?>
             {dataIndex : 'Phys Addr1'                 ,header : 'Address'          ,width : 100,id : 'physAddr1',sortable : true}
<?php
  }
?>
            ,{dataIndex : 'Town'                       ,header : 'Town'                                         ,sortable : true                             ,align : 'center'}
            ,{                                          header : 'Credit / Deficit'                                             ,renderer : markCreditDeficit,align : 'center'}
            ,{dataIndex : 'Load Credit/Deficit (lb/yr)',header : 'Removal (lb/yr)',width : 130                  ,sortable : true,renderer : absVal           ,align : 'center'}
            ,{dataIndex : 'Price ($)'                  ,header : 'Price ($)'                                    ,sortable : true,renderer : dollarFigure      ,align : 'right'}
            ,{dataIndex : 'Price ($/lb/yr)'            ,header : 'Price ($/lb/yr)'                              ,sortable : true,renderer : dollarFigure      ,align : 'right'}
          ]
          ,renderTo         : 'tradingGridPanel'
          ,autoWidth        : true
          ,height           : 395
          ,stripeRows       : true
<?php
  $col = $isRegistered ? 'ownerName' : 'physAddr1';
  echo $_REQUEST['page'] != 'introTrading' ? ",autoExpandColumn : '$col'" : ''
?>
          ,loadMask         : true
          ,remoteSort       : true
          ,sm               : new Ext.grid.RowSelectionModel({singleSelect:true})
          ,listeners        : {rowdblclick : function(grid,rowIndex,e) {
            submitTrading(grid.getStore().getAt(rowIndex));
          }}
        });
      }

      function submitTrading(rec) {
        new Ext.ux.ManagedIFrame.Window({
           defaultSrc  : 'popup.php'
             + '?blank0='
             + '&parcelNum=' + escape(rec.get('Parcel Num'))
             + '&ownerName=' + escape(rec.get('Owner Name'))
             + '&mailAddr=' + escape(makeNiceAddress(rec.get('Phys Addr1'),rec.get('Phys City'),rec.get('Phys State'),rec.get('Phys Zip')))
             + '&town=' + rec.get('Town')
             + '&creditDeficit=' + escape(markCreditDeficit('','',rec))
             + '&creditRemovealLbYr=' + absVal(rec.get('Load Credit/Deficit (lb/yr)'))
             + '&priceDollar=' + dollarFigure(rec.get('Price ($)'))
             + '&priceDollarLbYr=' + dollarFigure(rec.get('Price ($/lb/yr)'))
             + '&title=TRADING INFORMATION'
             + '&file=trading'
             + '&isRegistered=' + isRegistered
          ,focusOnLoad : true
          ,title       : name
          ,width       : 510
          ,height      : 400
          ,y           : 75
          ,bodyStyle   : 'background:white'
          ,constrainHeader : true
          ,modal           : true
          ,title           : 'Trading Information'
        }).show();
      }
    </script>

    <style>
      #tradingDiv {
        width      : <?php echo $_REQUEST['page'] == 'introTrading' ? 550 : 850?>px;
        height     : 420px;
        position   : relative;
      }
      #creditDeficit {
        left     : 0px;
        width    : <?php echo $_REQUEST['page'] == 'introTrading' ? 550 : 850?>px;
        height   : 20px;
        position : absolute;
      }
      #tradingGridPanel {
        top      : 25px;
        left     : 0px;
        width    : <?php echo $_REQUEST['page'] == 'introTrading' ? 550 : 850?>px;
        position : absolute;
      }
    </style>

    <div id="site">
<?php include_once('header.php')?>
        <div id="content">

            <div class="home-screen clearfix">

                <div class="left-column" style="width: 500px;">
                    <h2 style="margin-bottom: 20px;">Trading</h2>
                                <div id="tradingDiv">
                                  <div id="loading"><span id="loading-message">Loading. Please wait...</span></div>
                                  <div id="creditDeficit"></div>
                                  <div id="tradingGridPanel"></div>
                                </div>
               		</div>
               		<div class="right-column">
<?php
  if ($_REQUEST['page'] == 'introTrading') {
?>
                          <table id="blue-box">
                            <tr><th><div id="step-six" class="progress-bar">&nbsp;</div></th></tr>
                            <tr><td>
                              <div id="small_map"><img src="<?php echo $thumbnail?>"></div>
<?php
  if ($isRegistered) {
?>
                              <p><label class="item first">Property Owner:</label><label><?php echo substr($_COOKIE['ys-parcelOwner_Name'],2)?></label></p>
<?php
}
else {
?>
                              <p><label class="item first">Property Address:</label><label><?php echo substr($_COOKIE['ys-parcelPhys_Addr1'],2).', '.substr($_COOKIE['ys-parcelPhys_City'],2)?></label></p>
<?php
}
?>
                              <p>
                                <label class="item">Area (Acres)</label><br>
                                <label class="item">Total:</label><label><?php echo sprintf("%.2f",substr($_COOKIE['ys-parcelTotal_Area_(ac)'],2))?></label><br>
                                <label class="item">Impervious:</label><label><?php echo sprintf("%.2f",substr($_COOKIE['ys-parcelImperv_Area_(ac)'],2))?></label>
                              </p>
                              <p><label class="item">Stormwater Control:</label><label><?php echo substr($_COOKIE['ys-stormwaterFull'],2)?></label></p>
<?php
  $load = substr($_COOKIE['ys-loadCreditDeficit'],2);
  if ($load < 0) {
    echo "<p><label class=\"item\">Pollution Deficit:</label><label><font color=\"red\">$load</font></label></p>";
  }
  else {
    echo "<p><label class=\"item\">Pollution Credit:</label><label><font color=\"green\">+$load</font></label></p>";
  }
?>
                              <p><label class="item">Design Cost:</label><label><?php echo substr($_COOKIE['ys-totalCost'],2)?></label></p>
                              <p class="notice" id="notice"></p>
                            </td></tr>
                          </table>
<?php
  }
?>
               		</div>
               		<div style="clear: both;">
               			<a id="prevStep" href="#"><img src="images/back_but.png" style="vertical-align: top; margin-top: 10px;" alt="Back" title="Back" /></a>
									</div>

            </div>

        </div>
<?php include_once('footer.php')?>
    </div>
    <script type="text/javascript" src="./js/ext-3.4.0/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="./js/ext-3.4.0/ext-all.js"></script>
    <script type="text/javascript" src="./js/miframe/multidom.js"></script>
    <script type="text/javascript" src="./js/miframe/mif.js"></script>
    <script type="text/javascript" src="./js/calcs.js"></script>
    <script type="text/javascript" src="./js/renderers.js"></script>
  </body>
